package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.SpringSecurityUserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.SpringSecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = SpringSecurityUserService.class )
@Transactional
public class SpringSecurityUserServiceImpl implements SpringSecurityUserService {

    @Autowired
    private SpringSecurityUserDao springSecurityUserDao;


    @Override
    public User getByUsername(String username){
        return springSecurityUserDao.getByUsername(username);
    }
    @Override
    public List<Role> getByUserId(Integer userId){
        return springSecurityUserDao.getByUserId(userId);
    }

    @Override
    public List<Permission> getByRole(List<Role> roleList){
        List<Permission> list = new ArrayList<>();
        for (Role role : roleList) {
            if (role != null && role.getId() != null) {
                List<Permission> permissionList = springSecurityUserDao.getByRoleId(role.getId());
                for (Permission permission : permissionList) {
                    list.add(permission);
                }
            }

        }
        return list;
    }

}
